var synchrona =
{
  loaded: null,
  enabled: false,
  observerService : null,

  //initialization
  init: function (){
  	
    if (synchrona.loaded)
      return;
    else{
        synchrona.loaded = true;
        //coneect to jabber account
        
        //xmp listener
    	var channel = XMPP.createChannel();
    	
    		  
    	    channel.on({
    	        event     : 'message',
    	        direction : 'in',
    	        }, function(message) {
    	            receiveXMPP(message) });

        
        
        //listen for form submits on page load
        var appcontent = document.getElementById("appcontent");
        if(appcontent)
		{
        appcontent.addEventListener('load', synchrona.pageload, true);
		}
		//listen for XHR form submits
		  	var httpRequestObserver =
	{
    observe: function(aSubject, aTopic, aData)
	{
    if (aTopic == "http-on-examine-response" || aTopic == "http-on-modify-request" || aTopic == "http-on-examine-cached-response") {
        
        var newListener = new TracingListener();
        aSubject.QueryInterface(Components.interfaces.nsITraceableChannel);
        newListener.originalListener = aSubject.setNewListener(newListener);
    }
	},
    QueryInterface : function (aIID)
    {
        if (aIID.equals(Components.interfaces.nsIObserver) ||
            aIID.equals(Components.interfaces.nsISupports))
        {
            return this;
        }

        throw Components.results.NS_NOINTERFACE;

    }
	};
		observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
		
		try
		{
		observerService.addObserver(httpRequestObserver,"http-on-examine-response", false);
		observerService.addObserver(httpRequestObserver,"http-on-modify-request", false);
		//observerService.addObserver(httpRequestObserver,"http-on-examine-cached-response", false);
		}
		catch (err)
		{
		alert(err.toString());
		}
	}
},
 
//enable or disable synchrona    
enableSynchrona: function(){
var sts = synchrona.enabled;
//alert(sts);
var img;
	if(sts == true)
	{
	img=document.getElementById("synchronaIcon");
    img.src="chrome://synchrona/content/synchronaGrey.jpg";
    synchrona.enabled = false;
	}
    if(sts == false)
	{
    img=document.getElementById("synchronaIcon");
    img.src="chrome://synchrona/content/synchrona.jpg";
    synchrona.enabled = true;
	}

},
//intercept html form submits on page load
pageload: function (aEvent){
window.addEventListener('submit', synchrona.newsubmit, true);
 },

//capture and send form submits for blogger
newsubmit: function (event){ 
var form = event ? event.target : this;
var formaction = form.action;
//alert(formaction);
if (  synchrona.enabled ){
var msg = mapHtmlPost(formaction ,form);
if(msg!=null)
{
try
{
					dump("\n\nHTML FORM MSG:\n"+msg);
					sendXMPP(msg);
}
catch (exmpp)
{
alert("Error sending message over the network..");
}
}
}
},
 
windowClose: function (){
observerService.removeObserver(httpRequestObserver,"http-on-examine-response");
observerService.removeObserver(httpRequestObserver,"http-on-modify-request");
//observerService.removeObserver(httpRequestObserver,"http-on-examine-cached-response");
},

};


window.addEventListener('load', synchrona.init, false);
window.addEventListener('close', synchrona.windowClose, false);










